<?php

namespace App\Models;

class CartModel
{
    /**
     * 获取订单列表
     * entry CartExample
     * return array
     * */
    public static function getCartInsideUid($conn,$CartExample,$identical){
        $sql = "SELECT `id`,`serial`,`uid`,`bid`,`price`,`pay_price`,`pay_amount`,`integral`,`status`,`time`,`stime`,`ptime`,`addrid`,`pay_type`,`remarks`,`courier_number`,`type` FROM `cart` WHERE `uid` = $CartExample->uid and `status` in($CartExample->status) order by `time` desc limit $identical->pageStart,$identical->limit";
        return $conn->readConn($sql, true);
    }
    /**
     * 获取订单列表总数
     * entry CartExample
     * return array
     * */
    public static function getCartInsideUidCount($conn,$CartExample){
        $sql = "SELECT count(`id`) as count FROM `cart` WHERE `uid` = $CartExample->uid and `status` in($CartExample->status)";
        return $conn->readConn($sql)["count"];
    }
    /**
     * 通过id获取单条订单
     * entry CartExample
     * return array
     * */
    public static function getCartInsideId($conn,$CartExample){
        $sql = "SELECT `id`,`serial`,`uid`,`bid`,`price`,`pay_price`,`pay_amount`,`integral`,`status`,`time`,`addrid`,`type` FROM `cart` WHERE `id` = $CartExample->id and `uid` = $CartExample->uid";
        return $conn->readConn($sql);
    }
    /**
     * 通过cid获取单号
     * entry CartExample
     * return array
     * */
    public static function getCartSserialInsideId($conn,$cid){
        $sql = "SELECT `serial` FROM `cart` WHERE `id` = $cid";
        return $conn->readConn($sql)["serial"];
    }
    /**
     * 通过serial获取单条订单
     * entry CartExample
     * return array
     * */
    public static function getCartInsideSerial($conn,$CartExample){
        $sql = "SELECT `id`,`serial`,`uid`,`bid`,`price`,`pay_price`,`pay_amount`,`integral`,`status`,`time`,`addrid`,`type` FROM `cart` WHERE `serial` = '$CartExample->serial'";
        return $conn->readConn($sql);
    }
    /**
     * 生成订单
     * entry CartExample
     * return int
     * */
    public static function insertCart($conn,$CartExample){
        $insertSql ="INSERT INTO `cart`(`serial`,`uid`,`price`,`pay_price`,`integral`,`type`) VALUES ('$CartExample->serial',$CartExample->uid,$CartExample->price,$CartExample->pay_price,$CartExample->integral,$CartExample->type)";
        return $conn->executeConn($insertSql);
    }
    /**
     * 修改已支付订单
     * entry UserExample
     * return int
     * */
    public static function editPayCartInside($conn,$money,$integral,$pay_type,$CartExample){
        $upUserSql="UPDATE  `cart` SET `pay_amount` = $money,`integral` = $integral,`pay_type` = '$pay_type',`ptime` = NOW(),`status` = 3 WHERE `id` = $CartExample->id and `uid` = $CartExample->uid and `status` = $CartExample->status";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 修改订单
     * entry $CartExample
     * return int
     * */
    public static function editCartInside($conn,$CartExample){
        $joinSql="";
        if($CartExample->addrid !=null){
            $joinSql.=" `addrid` = '$CartExample->addrid',";
        }
        if($CartExample->pay_type !=null){
            $joinSql.=" `pay_type` = '$CartExample->pay_type',";
        }
        if($CartExample->remarks !=null){
            $joinSql.=" `remarks` = '$CartExample->remarks',";
        }
        if($CartExample->stime !=null){
            $joinSql.=" `stime` = $CartExample->stime,";
        }
        if($CartExample->status !=null){
            $joinSql.=" `status` = '$CartExample->status',";
        }
        if($CartExample->courier_number !=null){
            $joinSql.=" `courier_number` = '$CartExample->courier_number',";
        }
        if($CartExample->type !=null){
            $joinSql.=" `type` = '$CartExample->type',";
        }
        if($joinSql !=null){
            $innerJoin = rtrim($joinSql,",");
            $upUserSql="UPDATE  `cart` SET $innerJoin WHERE `id` in($CartExample->id) and `uid` = $CartExample->uid";
            return $conn->executeConn($upUserSql);
        }
    }
}